package net.bxx2004.zephyr.network;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.util.Identifier;

public class TMessage{
    public final String channel;
    public TMessage(String channel){
        ClientPlayNetworking.registerGlobalReceiver(new Identifier(channel), (client, handler, buf, responseSender) -> {
            byte[] bytes = buf.getWrittenBytes();
            client.execute(() -> {
                MessageEvent.EVENT.invoker().interact(client.player,new MessageBox(channel, new String(bytes)));
            });
        });
        this.channel = channel;
    }
    public void send(MessageBox box){
        ByteBuf buf = Unpooled.wrappedBuffer(box.origin());
        ClientPlayNetworking.send(new Identifier(channel),new PacketByteBuf(buf));
    }
}
